package cn.gailvlun.gll.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import cn.gailvlun.gll.BuildConfig;
import cn.gailvlun.gll.net.GsonUtil;



public class QQLogin extends BaseThirdLogin {

    private Tencent tencent;
    private Context mContext;

    public QQLogin(Context context) {
        mContext = context;
        tencent = Tencent.createInstance(BuildConfig.QQ_APP_ID, context);
    }

    @Override
    public boolean isAppInstalled() {
        return tencent.isQQInstalled(mContext);
    }

    @Override
    public void login() {
        if (!tencent.isSessionValid()) {
            tencent.login((Activity) mContext, "all", mIUiListener);
        } else {
            
            ToastUtil.showShort("有效");
        }
    }

    private IUiListener mIUiListener = new IUiListener() {

        @Override
        public void onComplete(Object o) {
            QQLoginRes qqLoginRes = GsonUtil.getGson().fromJson(o.toString(), QQLoginRes.class);
            mOnLoginListener.onSuccess(qqLoginRes.getAccess_token());
        }

        @Override
        public void onError(UiError uiError) {



            mOnLoginListener.onFailure(uiError.errorMessage);
        }

        @Override
        public void onCancel() {
            mOnLoginListener.onFailure("已取消");
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
    }
}
